 <div class="filter">
 	<?php if(isset($displayCols[0])): ?>
    <select class="filters_col" id="media_buytype" name="buytype">
        <option value="">---Buy Type---</option>
       <?php foreach($filters['buy_type'] as $bt): ?>
            <option value="<?php echo $bt; ?>"><?php echo $bt; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[1])): ?>
    <select class="filters_col" id="media_mediatype" name="mediatype">
        <option value="">---Media Type---</option>
        <?php foreach($filters['media_type'] as $mt): ?>
            <option value="<?php echo $mt; ?>"><?php echo $mt; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[2])): ?>
    <select class="filters_col" id="media_publisher" name="publisher">
        <option value="">---Publisher---</option>
        <?php foreach($filters['pub'] as $pub): ?>
            <option value="<?php echo $pub; ?>"><?php echo $pub; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[3])): ?>
    <select class="filters_col" id="media_pla" name="pla">
        <option value="">---Placement---</option>
        <?php foreach($filters['pla'] as $pla): ?>
            <option value="<?php echo $pla; ?>"><?php echo $pla; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[4])): ?>
    <select class="filters_col" id="media_campaign" name="campaign">
        <option value="">---Campaign---</option>
        <?php foreach($filters['cam'] as $cam): ?>
            <option value="<?php echo $cam; ?>"><?php echo $cam; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[5])): ?>
    <select class="filters_col" id="media_creative" name="creative">
        <option value="">---Creative---</option>
        <?php foreach($filters['cre'] as $cre): ?>
            <option value="<?php echo $cre; ?>"><?php echo $cre; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <?php if(isset($displayCols[6])): ?>
    <select class="filters_col" id="media_monetization" name="monetization">
        <option value="">---Monetization---</option>
        <?php foreach($filters['monetization'] as $monetization): ?>
            <option value="<?php echo $monetization; ?>"><?php echo $monetization; ?></option>
        <?php endforeach; ?>
    </select><?php endif; ?>
    <input type="button" id="media_filter" value=">>>Filter" />
</div>

<table class="tablesorter" id="tab0" >
    <thead>
        <tr>
            <th class="col_date">Date<span class="close">X</span></th>
            <?php if(isset($displayCols[0])):?> <th class="col_buy">Buy Type<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[1])):?> <th class="col_media">Publisher Type<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[2])):?> <th class="col_publisher">Publisher<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[3])):?> <th class="col_placement">Placement<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[4])):?> <th class="col_campaign">Campaign<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[5])):?> <th class="col_creative">Creative<span class="close">X</span></th> <?php endif;?>
            <?php if(isset($displayCols[6])):?> <th class="col_monetization">Monetization<span class="close">X</span></th> <?php endif;?>
            <th class="col_impression">Impressions<span class="close">X</span></th>
            <th class="col_clicks">Clicks<span class="close">X</span></th>
            <th class="col_ctr_percentage">CTR %<span class="close">X</span></th>
            <th class="col_cpc">CPC<span class="close">X</span></th>
            <th class="col_lead">Leads<span class="close">X</span></th>
            <th class="col_paidconversion">Paid Conversion<span class="close">X</span></th>
            <th class="col_actualconversion">Actual Conversions<span class="close">X</span></th>
            <th class="col_scrubrate">Scrub Rate<span class="close">X</span></th>
            <th class="col_cpl">CPL<span class="close">X</span></th>
            <th class="col_conversion_percentage">Conversion %<span class="close">X</span></th>
            <th class="col_revenue">Revenue<span class="close">X</span></th>
            <th class="col_cost">Cost<span class="close">X</span></th>
            <th class="col_gross">Gross Profit<span class="close">X</span></th>
            <th class="col_gm">GM<span class="close">X</span></th>
            <th class="col_roas">ROAS<span class="close">X</span></th>
            <th class="col_cpm">CPM<span class="close">X</span></th>
            <th class="col_rpm">RPM<span class="close">X</span></th>
            <th class="col_rpc">RPC<span class="close">X</span></th>
            <th class="col_rpl">RPL<span class="close">X</span></th>
        </tr>
    </thead>
    <tbody>
        <?php 
            if((!empty($reportItems)) && $reporttype=="0"):
                $durations = Util::getReportDuration($duration);
                foreach ($reportItems as $k=>$reportItem):
                    $grossProfit = $reportItem['revenue'] - $reportItem['cost'];
        
					list($date['start'], $site) = explode('|', $reportItem['date']);
					if($site == '' && isset($displayCols[7])){ continue; }
					if($interval == 2){
						list($year, $week) = str_split($date['start'], 4);
						$date = Util::getStartAndEndDate($week, $year);
					}
		?>
                <tr>
                    <td class="col_date"><?php echo $date['start'];?> <?php if($interval==2){ echo '---'.$date['end'];} ?></td>
                    <?php if(isset($displayCols[0])): ?> <td class="col_buy"><?php echo $reportItem['buy_type'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[1])): ?> <td class="col_media"><?php echo $reportItem['media_type'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[2])): ?> <td class="col_publisher" id="pub<?php echo $k; ?>"><?php echo $reportItem['pub'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[3])): ?> <td class="col_placement" id="pla<?php echo $k; ?>"><?php echo $reportItem['pla'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[4])): ?> <td class="col_campaign" id="cam<?php echo $k; ?>"><?php echo $reportItem['cam'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[5])): ?> <td class="col_creative"><?php echo $reportItem['cre'];?></td> <?php endif; ?>
                    <?php if(isset($displayCols[6])): ?> <td class="col_monetization"><?php echo $reportItem['monetization'];?></td> <?php endif; ?>
                    <td class="col_impression"><?php echo $reportItem['impression'];?></td>
                    <td class="col_clicks"><?php echo $reportItem['click'];?></td>
                    <td class="col_ctr_percentage"><?php echo Util::divide($reportItem['click'], $reportItem['impression'], 2, true);?></td>
                    <td class="col_cpc"><?php echo Util::divide($reportItem['cost'], $reportItem['click'], 2);?></td>
                    <td class="col_lead"><?php echo $reportItem['leads'];?></td>
                    <td class="col_paidconversion"><?php echo $reportItem['conversion'];?></td>
                    <td class="col_actualconversion"><?php echo $reportItem['conversion'] + $reportItem['suppress'];?></td>
                    <td class="col_scrubrate"><?php echo number_format((1 - Util::divide($reportItem['conversion'], ($reportItem['conversion'] + $reportItem['suppress']), 4))*100, 2); ?>%</td>
                    <td class="col_cpl"><?php echo Util::divide($reportItem['cost'], $reportItem['leads'], 2); ?></td>
                    <td class="col_conversion_percentage"><?php echo Util::divide($reportItem['leads'], $reportItem['click'], 2, true);?></td>
                    <td class="col_revenue">$ <?php echo $reportItem['revenue'];?></td>
                    <td class="col_cost">$ <?php echo $reportItem['cost'];?></td>
                    <td class="col_gross"><label id=<?php echo $k; ?> class="addpixelbtn" style="text-decoration:underline; color:#06F; cursor:pointer;">$ <?php echo number_format($grossProfit, 2); ?></label></td>
                    <td class="col_gm"><?php echo Util::divide($grossProfit , $reportItem['revenue'],  2, true);?></td>
                    <td class="col_roas"><?php echo Util::divide($grossProfit , $reportItem['cost'], 2, true);?></td>
                    <td class="col_cpm"><?php echo Util::divide($reportItem['cost'] * 1000, $reportItem['impression'], 2);?></td>
                    <td class="col_rpm"><?php echo Util::divide($reportItem['revenue'] * 1000, $reportItem['impression'], 2);?></td>
                    <td class="col_rpc"><?php echo Util::divide($reportItem['revenue'], $reportItem['click'], 2);?></td>
                    <td class="col_rpl"><?php echo Util::divide($reportItem['revenue'], $reportItem['leads'], 2);?></td>
                </tr>
            <?php endforeach;?>
        <?php endif;?>
    </tbody>
</table>

<script type="text/javascript" src="<?php echo JS_DIR; ?>jquery.tablesorter.js"></script>
<script type="text/javascript" src="<?php echo JS_DIR; ?>jquery.uitablefilter.js"></script>
<script type="text/javascript">

var $table = $('#tab0');
$table.tablesorter();

$('#media_filter').click(function(){
	var id_to_header={"media_buytype":"Buy Type",
					  "media_mediatype":"Publisher Type",
					  "media_publisher":"Publisher",
					  "media_pla":"Placement",
					  "media_campaign":"Campaign",
					  "media_creative":"Creative",
					  "media_monetization":"Monetization"
					 };
	
	var $table = $('#tab0');
	var filters = new Array();
	var elems = $('.filters_col');
	elems.each(function(){
		var elem = $(this);
		if(elem.css('display') != 'none'){
			if(elem.val() != ""){
				filters[elem.val()] = id_to_header[elem.attr('id')];
			}
		}
	});
	$.uiTableFilter( $table, filters);
});
</script>